package com.yxplearn.mybatisplus.mybatis_sample;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.lang.reflect.Method;
import java.util.Properties;

@Intercepts(
        @Signature(type = Executor.class, method = "query", args = {
                MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class
        }))
public class SimpleInterceptor2 implements Interceptor {

    private String name;

    public SimpleInterceptor2(String name) {
        this.name = name;
    }

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        System.out.println("intercept 执行前" + name);
        // 获取需要拦截的方法
        Method method = invocation.getMethod();
        // 获取需要拦截的对象
        Executor executor = (Executor) invocation.getTarget();
        // 获得拦截器的参数 4或者6个
        Object[] args = invocation.getArgs();
        MappedStatement ms = (MappedStatement) args[0];
        Object parameter = args[1];
        RowBounds rowBounds = (RowBounds) args[2];
        ResultHandler resultHandler = (ResultHandler) args[3];
        Object proceed = invocation.proceed();
        System.out.println("intercept 执行前" + name);
        return proceed;
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {
        properties.setProperty("name", "xxppx");
        properties.setProperty("email", "Ffxxs");
    }
}
